/*
 *	               _ooOoo_ 
 *	              o8888888o 
 *	              88" . "88 
 *	              (| -_- |) 
 *	              O\  =  /O 
 *	           ____/`---'\____ 
 *	         .'  \\|     |//  `. 
 *	        /  \\|||  :  |||//  \ 
 *	       /  _||||| -:- |||||-  \ 
 *	       |   | \\\  -  /// |   | 
 * 	       | \_|  ''\---/''  |   | 
 * 	       \  .-\__  `-`  ___/-. / 
 * 	    ___ `. .'  /--.--\  `. . __ 
 *	  ."" '<  `.___\_<|>_/___.'  >'"". 
 *	 | | :  `- \`.;`\ _ /`;.`/ - ` : | | 
 *	 \  \ `-.   \_ __\ /__ _/   .-` /  / 
 *======`-.____`-.___\_____/___.-`____.-'====== 
 *                   `=---=' 
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
 *佛祖保佑       永无BUG 
 * File name:         CodeImage.java
 * Copyright@blog.ilvelh.com(China)
 * Editor:           JDK1.7_40
 */
package com.ilvelh.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 生成验证码图片的工具类
 * <p>
 * Author: ilveh
 * <p>
 * Date:2013-7-22 Time:下午4:22:21
 * <p>
 */
public class CodeImage {
	public static BufferedImage getImage(String vcode) {
		Random random = new Random();
		int width = 100, height = 25;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics g = image.getGraphics();
		// 设定背景色
		g.setColor(new Color(0xDCDCDC));
		g.fillRect(0, 0, width, height);
		// 画边框
		g.setColor(Color.black);
		g.drawRect(0, 0, width - 1, height - 1);
		int x, y;
		g.setColor(Color.black);

		g.setFont(new Font("Atlantic Inline", Font.PLAIN, 30));
		String str = vcode.substring(0, 1);
		x = random.nextInt(8);
		y = random.nextInt(17);
		g.drawString(str, 8, 22);
		str = vcode.substring(1, 2);
		g.drawString(str, 30, 20);
		str = vcode.substring(2, 3);
		g.drawString(str, 48, 25);
		str = vcode.substring(3, 4);
		g.drawString(str, 65, 23);
		// 随机产生600个干扰点，使图象中的认证码不易被其它程序探测到
		int rc, gc, bc;
		for (int i = 0; i < 100; i++) {
			x = random.nextInt(width);
			y = random.nextInt(height);
			rc = random.nextInt(255);
			gc = random.nextInt(255);
			bc = random.nextInt(255);
			g.setColor(new Color(rc, gc, bc));
			g.drawOval(x, y, 0, 0);
		}
		// 图象生效
		g.dispose();
		return image;
	}
}
